<template>
  <div>
    <home-header></home-header>
    <home-swiper :list="swiperList"></home-swiper>
    <home-icons :list="iconList"></home-icons>
    <home-recommend :list="recommendList"></home-recommend>
    <home-weekend :list="weekendList"></home-weekend>
  </div>
</template>

<script>
// @ is an alias to /src
import HomeHeader from '../../components/home/Header'
import HomeSwiper from '../../components/home/Swiper'
import HomeIcons from '../../components/home/Icons'
import HomeRecommend from '../../components/home/Recommend'
import HomeWeekend from '../../components/home/Weekend'
import axios from 'axios'
import { mapState } from "vuex"
export default {
  name: 'home',
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  data () {
    return {
      lastCity: "",
      swiperList: [],
      iconList: [],
      recommendList: [],
      weekendList: []
    }
  },
  computed: {
    // 可以通过this.city
    ...mapState(["city"])
  },
  mounted () {
    // 使用 keep-alive之后,会缓存起来
    this.lastCity = this.city
    console.log("mounted") // eslint-disable-line
    this.getHomeInfo()
  },
  activated () {
    // 使用了 keep-alive 之后会多出这个生命周期方法,它是在页面重新被显示的时候会重新调用
    if (this.lastCity !== this.city) {
      this.lastCity = this.city
      this.getHomeInfo()
    }
    console.log("activated") // eslint-disable-line
  },
  methods: {
    getHomeInfo () {
      axios.get('/api/index.json?city=' + this.city).then(this.getHomeInfoSucc)
    },
    getHomeInfoSucc (res) {
      console.log(res); //eslint-disable-line 
      res = res.data
      if (res.ret && res.data) {
        const data = res.data
        this.swiperList = data.swiperList
        this.iconList = data.iconsList
        this.recommendList = data.recommendList
        this.weekendList = data.weekendList
      }
    }
  }
}
</script>
